package org.jeecg.modules.xindian.reconciliation.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.time.DateUtils;
import org.jeecg.modules.xindian.reconciliation.entity.InsuranceStatistics;
import org.jeecg.modules.xindian.reconciliation.entity.InsuranceXindianReconciliation;
import org.jeecg.modules.xindian.reconciliation.service.InsuranceXindianReconciliationService;
import org.jeecg.modules.xindian.reconciliation.mapper.InsuranceXindianReconciliationMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
* @author Administrator
* @description 针对表【insurance_xindian_reconciliation(新点对账文件)】的数据库操作Service实现
* @createDate 2024-09-05 10:58:22
*/
@Service
public class InsuranceXindianReconciliationServiceImpl extends ServiceImpl<InsuranceXindianReconciliationMapper, InsuranceXindianReconciliation>
    implements InsuranceXindianReconciliationService{

    @Override
    public void processData() {
        List<InsuranceXindianReconciliation> guangrun = loadData("guangrun");
        List<InsuranceXindianReconciliation> xierong = loadData("xierong");
        List<InsuranceXindianReconciliation> zhonglian = loadData("zhonglian");
        processData("guangrun",zhonglian,guangrun);
        processData("xierong",zhonglian,xierong);
    }


    public List<InsuranceXindianReconciliation> loadData(String dataSource){
        try{
            DynamicDataSourceContextHolder.push(dataSource);
            return lambdaQuery().list();
        }finally {
            DynamicDataSourceContextHolder.poll();
        }
    }


    public void processData(String dataSource,List<InsuranceXindianReconciliation> source,List<InsuranceXindianReconciliation> tag){
        try{
            DynamicDataSourceContextHolder.push(dataSource);
            Map<String,InsuranceXindianReconciliation>  tagMap = tag.stream().collect(Collectors.toMap(InsuranceXindianReconciliation::getApplyno, Function.identity()));
            List<InsuranceXindianReconciliation> add = new ArrayList<>();
            List<InsuranceXindianReconciliation> update = new ArrayList<>();
            for (InsuranceXindianReconciliation rec:source) {
                InsuranceXindianReconciliation   one  =  tagMap.get(rec.getApplyno());
                if(one ==null){
                    add.add(rec);
                }else {
                    String id =  one.getId();
                    BeanUtils.copyProperties(rec,one);
                    one.setId(id);
                    update.add(one);
                }
            }
            if(ObjectUtil.isNotEmpty(add)){
                saveBatch(add);
            }
            if(ObjectUtil.isNotEmpty(update)){
                updateBatchById(update);
            }
        }finally {
            DynamicDataSourceContextHolder.poll();
        }
    }
}




